
var Resellers = new Class({
    Implements: Options,
    options: {
        'tagid':''
    },
    initialize: function(opts) {
        this.setOptions(opts);

        this.panel = document.id(this.options.tagid);
        if(!this.panel) return;
        this.panel.empty();

        this.begDate = this.endDate = new Date().format('%Y-%m-%d'); // set range date
        this.content = '';  // main include listData + before calendar and pager
        this.listData = ''; // tag include (head,bottom,data)
        this.curPage = 0; // current page
        this.calendar = function(){};
        this.pager = function(){};

        this.createResellersTable();

        this.resRequest = new Request({
            url: '/Admin/webstat/getFromMySQL',
            method: 'get',
            onRequest: function() {
                new ShowBox().say('Loading...');
                new Loader().show();
            },
            onComplete: function(resp) {
                var data;
                try {
                    data = eval('(' + resp + ')');
                }
                catch(err) { alert('Error...' + err); return; }
                new Loader().hide();

                this.fillContent(data);
            }.bind(this)
        }).send('table=resellers&beg=' + this.begDate + '&end=' + this.endDate  + '&nc=' + Date.now());

    },
    createResellersTable: function() {
        this.content = new Element('div#content');
        this.listData = new Element('table#listData[rules=groups]');

        this.panel.adopt(
            new Element('div#legend[html=<b>Legend:</b>&nbsp;<tt style="background-color:#87d4bf;">&nbsp;reseller;&nbsp;</tt>]'),
            new Element('div.tl[html=<div class="tr"><div>&nbsp;</div></div>]'),
            this.content.adopt(this.listData),
            new Element('div.bl[html=<div class="br"><div>&nbsp;</div></div>]')
        )

        var calendar = new Element('td.left');
        var pager = new Element('td.right');
        new Element('table#pager').adopt(
            new Element('tbody').adopt(
                new Element('tr').adopt(
                    calendar,
                    pager
                )
            )
        ).inject(this.content, 'top');

        this.calendar = new Calendar({
            'tagid': calendar,
            onSelect: function(bedDate, endDate) {
                this.begDate = bedDate;
                this.endDate = endDate;
                this.curPage = 0;

                this.resRequest.send('table=resellers&curPage=0&beg=' + this.begDate + '&end=' + this.endDate + '&nc=' + Date.now());
            }.bind(this)
        });

        this.pager = new Pager({
            'tagid': pager,
            onNewpage: function(currentPage) {
                this.curPage = currentPage;

                this.resRequest.send('table=resellers&beg=' + this.begDate + '&end=' + this.endDate + '&curPage=' + this.curPage + '&nc=' + Date.now());
            }.bind(this)
        });

        this.createHead();
    },
    createHead: function() {
        this.listData.adopt(
            new Element('thead').adopt(
                new Element('tr').adopt(
                    new Element('th.brdCol.w15[html=&nbsp;]'),
                    new Element('th.brdCol.w80[text=Certificate]'),
                    new Element('th#srchead.brdCol.w120[text=Accware#]').store('tbname','accware'),
                    new Element('th.brdCol.w120[text=Name]'),
                    new Element('th#srchead.brdCol.w80[text=Email]').store('tbname','email'),
                    new Element('th.brdCol.w120[text=Phone]')
                )
            ),
            new Element('tfoot').adopt(
                new Element('tr').adopt(
                    new Element('th.tdFull[colspan=6][html=&nbsp;]')
                )
            ),
            new Element('tbody')
        );

        new Suggest({'url': '/Admin/webstat/getFromMySQL',
            'tagid': this.options.tagid,
            'table': 'sugcust',
            'minLength': 0,
            onChoice: function(custid) {

                this.resRequest.send('table=resellers&custid=' + custid + '&nc=' + Date.now());
            }.bind(this)
        });

    },
    fillContent: function(data) {
        this.pager.setData(this.curPage, data.cntPage, data.onPage, data.rows.length);

        var tbody,cust,tr,td,color=false;

        tbody = this.listData.getElement('tbody');
        tbody.empty();

        if(!data.rows.length) {
            new Element('tr').adopt(
                new Element('th.brdCol.red[colspan=6][text=Resellers From: ' + this.begDate +
                            ' To: ' + this.endDate + ' not find, select new date or use search.][style=height:20px;]'
                )
            ).inject(tbody);
        }
        else {

            for(var i = 0; i < data.rows.length; i++) {
                cust = data.rows[i];

                tr = new Element('tr[style=background-color:#' + ((color = !color) ? 'ffffff' : 'fcffcd') + ';]',{
                    'events': {
                        'mouseenter': function() {this.addClass('rowHover');},
                        'mouseleave': function() {this.removeClass('rowHover');}
                    }
                });

                td = new Element('td.imgCol' + (cust.type == 'R' ? '.bteal' : '') + ']').adopt(
                    new Element('a.watch[href=javascript:;][html=&nbsp;][title=Change Approve]',{
                        'events': {
                            'click': this.getResellerInfo.bind(this, cust.id),
                            'focus': function() {this.blur();}
                        }
                    })
                );

                tbody.adopt(
                    tr.adopt(
                        td,
                        new Element('td.brdCol.center.w80[text=' + cust.code + ']'),
                        new Element('td.brdCol.center.w80[text=' + cust.accw + ']'),
                        new Element('td.brdCol.left.w120[text=' + cust.name + ']'),
                        new Element('td.brdCol.left.w80[text=' + cust.email + ']'),
                        new Element('td.brdCol.center.w120[text=' + cust.bphone + ']')
                    )
                );
            }// for
        }//else

    },
    getResellerInfo: function(resellerid, ev) {
        ev.stop();

        new ResellerInfo({
            'resellerid': resellerid,
            'targetid': ev.target,
            onReload: function(custid) {

                this.resRequest.send('table=resellers&beg=' + this.begDate + '&end=' + this.endDate + '&curPage=' + this.curPage + '&nc=' + Date.now());
            }.bind(this)
        });
    }
});

var ResellerInfo = new Class({
    Implements: [Events, Options],
    options: {
        resellerid: null,
        target: null
    },
    initialize: function(options) {
        this.setOptions(options);
        if(!this.options.resellerid) return;

        // fill box
        new Request({
            url: '/Admin/webstat/getFromMySQL',
            method: 'get',
            onRequest: function() {
                this.options.targetid.addClass('load');
            }.bind(this),
            onComplete: function(resp) {
                var data = [];
                try {
                    data = eval('(' + resp + ')');
                }
                catch(err) { alert('Error...\n' + err); return; }
                this.options.targetid.removeClass('load');

                this.makeForm(data);
            }.bind(this)
        }).send('table=resinfo&custid=' + this.options.resellerid + '&nc=' + Date.now());

    },
    makeForm: function(res) {

        // input form element
        this.accwareField = new Element('input[type=text][name=accware][size=30][maxlength=10][value=' + res.accw + ']');
        this.checkbox = new Element('input[type=checkbox][value=' + res.type + ']').set('checked', res.type == 'R');
        this.textarea = new Element('textarea[rows=5][name=notice][html=' + res.note + '][style=width:400px;]');

        this.submit = new Element('input.closeThis[type=submit][value=Submit]');

        //make body
        var table = new Element('table#resinfo').adopt(
            new Element('tbody').adopt(
                new Element('tr').adopt(
                    new Element('td.head[text=Email :]'),
                    new Element('td.data[text=' + res.email + ']'),
                    new Element('td[html=&nbsp;]'),
                    new Element('td.head[text=Accware :]'),
                    new Element('td.data').adopt(this.accwareField)
                ),
                new Element('tr').adopt(
                    new Element('td.head[text=Certificate #]'),
                    new Element('td.data[text=' + res.code + ']'),
                    new Element('td[html=&nbsp;]'),
                    new Element('td.head[text=Joined :]'),
                    new Element('td.data[text=' + Date.parse(res.date).format('%b %d,%Y %H:%M:%S %a') + ']')
                ),
                new Element('tr').adopt(
                    new Element('td.head[text=Count orders :]'),
                    new Element('td.data[text=' + res.orders + ']'),
                    new Element('td[html=&nbsp;]'),
                    new Element('td.head[text=Sum orders :]'),
                    new Element('td.data[text=$' + numberFormat(res.sum) + ']')
                ),
                new Element('tr').adopt(
                    new Element('td.sep[colspan=5][html=<hr/>]')
                ),
                new Element('tr[style=text-align:center;]').adopt(
                    new Element('td[colspan=2][html=<b>Billing</b>]'),
                    new Element('td[html=&nbsp;]'),
                    new Element('td[colspan=2][html=<b>Shipping</b>]')
                ),
                new Element('tr').adopt(
                    new Element('td.head[text=Company :]'),
                    new Element('td.data[text=' + res.billing.company + ']'),
                    new Element('td[html=&nbsp;]'),
                    new Element('td.head[text=Company :]'),
                    new Element('td.data[text=' + res.shipping.company + ']')
                ),
                new Element('tr').adopt(
                    new Element('td.head[text=Name :]'),
                    new Element('td.data[text=' + res.billing.name + ']'),
                    new Element('td[html=&nbsp;]'),
                    new Element('td.head[text=Name :]'),
                    new Element('td.data[text=' + res.shipping.name + ']')
                ),
                new Element('tr').adopt(
                    new Element('td.head[text=Address :]'),
                    new Element('td.data[html=' + res.billing.address1 + (res.billing.address2 ? '<br/>' + res.billing.address2:'') + ']'),
                    new Element('td[html=&nbsp;]'),
                    new Element('td.head[text=Address :]'),
                    new Element('td.data[html=' + res.shipping.address1 + (res.shipping.address2 ? '<br/>' + res.shipping.address2:'') + ']')
                ),
                new Element('tr').adopt(
                    new Element('td'),
                    new Element('td.data[html=' + res.billing.address + ']'),
                    new Element('td[html=&nbsp;]'),
                    new Element('td'),
                    new Element('td.data[text=' + res.shipping.address + ']')
                ),
                new Element('tr').adopt(
                    new Element('td.head[text=Phone :'),
                    new Element('td.data[text=' + res.billing.phone + ']'),
                    new Element('td[html=&nbsp;]'),
                    new Element('td.head[text=Phone :]'),
                    new Element('td.data[text=' + res.shipping.phone + ']')
                ),
                new Element('tr').adopt(
                    new Element('td.head[text=Cell :]'),
                    new Element('td.data[text=' + res.billing.cell + ']'),
                    new Element('td[html=&nbsp;]'),
                    new Element('td.head[text=Cell :]'),
                    new Element('td.data[text=' + res.shipping.cell + ']')
                ),
                new Element('tr').adopt(
                    new Element('td.head[text=Fax :]'),
                    new Element('td.data[text=' + res.billing.fax + ']'),
                    new Element('td[html=&nbsp;]'),
                    new Element('td.head[text=Fax :]'),
                    new Element('td.data[text=' + res.shipping.fax + ']')
                ),
                new Element('tr').adopt(new Element('td.sep[colspan=5][html=<hr/>]')),
                new Element('tr').adopt(new Element('td.note[colspan=5][text=Notice :]')),
                new Element('tr').adopt(
                    new Element('td.texta[colspan=5]').adopt(this.textarea)
                ),
                new Element('tr').adopt(
                    new Element('td.head[colspan=2]').adopt(
                        new Element('label[html=Approve :&nbsp;]').adopt(
                            this.checkbox
                        )
                    ),
                    new Element('td[html=&nbsp;]'),
                    new Element('td.data[colspan=2]').adopt(this.submit)
                )
            )
        );

        new ModalBox({
            width: 510,
            modal: {
                background: '#555'
            },
            onOpen: function() {
                this.submit.addEvent('click', this.sendApprove.bind(this, res.id, res.email));
            }.bind(this)
        }).doBox('<h2>' + (res.type == 'R' ? 'Reseller' : 'Customer') + ' Info</h2>', new Element('center').adopt(table));

    },
    sendApprove: function(resellerid, email) {

        // write to database
        new Request({
            url: '/Admin/webstat/getFromMySQL',
            method: 'get',
            onRequest: function() {
                new Loader().show();
            },
            onComplete: function() {
                new Loader().hide();
            }
        }).send('table=ressave&custid=' + resellerid +
                '&accw=' +  this.accwareField.get('value') +
                '&email=' + email +
                '&ckbox=' + (this.checkbox.get('checked') ? 'R' : 'C') +
                '&note=' +  this.textarea.get('value') +
                '&nc=' + Date.now());

        this.fireEvent('reload', [this.options.resellerid]);

    }
});
